const registerUrl = "https://ali.ikenway.net:18443/api/auth/register";
const validateUrl = "https://ali.ikenway.net:18443/api/auth/validate"


function callback(res) {
    let validateData = {
        "ticket": res.ticket,
        "randStr": res.randstr
    };

    fetch(validateUrl, {
        method: 'POST',
        headers: {
            'Content-Type': "application/json"
        },
        body: JSON.stringify(validateData)
    }).then(validateRes => {
        validateRes.json().then(validateJson => {
            if (validateJson.code == 200) {
                let data = new URLSearchParams();
                data.append("username", document.getElementById("inputEmail").value);
                data.append("password", document.getElementById("inputPassword").value);
                data.append("passwordDouble", document.getElementById("inputPasswordDouble").value);

                register(data);
            } else {
                alertMsg(validateJson.msg);
            }
        })
    })
}

function register(data) {
    fetch(registerUrl, {
        method: "POST",
        body: data
    }).then(res => {
        res.json().then(json => {
            console.log(json)

            alertMsg(json.msg);
            
            if (json.code == 200) {
                setTimeout(function () {
                    window.location.href = loginPageUrl;
                }, 2000)
            }
        })
    })
}